package com.obelisco.vista.controles;

import java.lang.reflect.InvocationTargetException;
import java.util.Iterator;
import java.util.List;
import java.util.Vector;

import nextapp.echo2.app.Extent;
import nextapp.echo2.app.event.ListDataEvent;
import nextapp.echo2.app.event.ListDataListener;
import nextapp.echo2.app.list.DefaultListModel;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.MethodUtils;

import echopointng.ListBoxEx;
import echopointng.SelectFieldEx;

public class ListBox extends ListBoxEx {
	
	private List objetos;
	private String  atributo;
	private String  id;
	private DefaultListModel modelo;
	
	private int ancho = 100;
	private int alto = 120;
	
	public ListBox(int ancho, int alto) {
		super();
		this.ancho = ancho;
		this.alto = alto;
		initComponents();
	}

	private void initComponents() {
		this.setStyleName("Obelisco.ListBox");
		setWidth(new Extent(ancho));
		setHeight(new Extent(alto));
	}
	
	public void removeItems()
	{
		if (modelo != null)
		{
			modelo.removeAll();
		}
		
	}
	
	
	
	private void agregarModelo(){		
		try {
			modelo = new DefaultListModel();
			
			Object valorAtributo;
			
			for (Iterator i = this.objetos.iterator(); i.hasNext();) {
				if (this.atributo != null)
				{
					Object item = (Object) i.next();
					valorAtributo = BeanUtils.getProperty(item, this.atributo);
				}
				else
				{
					valorAtributo = (String) i.next();
				}
				
				modelo.add(valorAtributo);
			}
			this.setModel(modelo);
			this.setSelectedIndex(0);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public List getItems() {
		return objetos;
	}

	public void setItems(List listaObjetos, String nombreAtributo, String nombreCampoClave) {
		this.objetos = listaObjetos;
		this.atributo = nombreAtributo;
		this.id = nombreCampoClave;
		this.agregarModelo();
	}

	public void setItems(List listaObjetos) {
		this.objetos = listaObjetos;
		this.agregarModelo();
	}
	
	public String getAtributo() {
		return atributo;
	}

	public void setAtributo(String nombreAtributo) {
		this.atributo = nombreAtributo;
	}
	
	public String getId() {
		return id;
	}
	
	public void setId(String nombreCampoClave) {
		this.id = nombreCampoClave;
	}
	
}